#include "proxydescription.h"
#include "ui_proxydescription.h"

ProxyDescription::ProxyDescription(int index, QWidget *parent) :
    QFrame(parent),
    ui(new Ui::ProxyDescription)
{
    ui->setupUi(this);
    ui->indexLabel->setText(QString().number(index));

    waitWidget=NULL;
    this->index=index;
    clearWaits();
}

void ProxyDescription::setType(int tp){
    _type=tp;
//    QNetworkProxy::NoProxy	2	No proxying is used
//    QNetworkProxy::DefaultProxy	0	Proxy is determined based on the application proxy set using setApplicationProxy()
//    QNetworkProxy::Socks5Proxy	1	Socks5 proxying is used
//    QNetworkProxy::HttpProxy	3	HTTP transparent proxying is used
//    QNetworkProxy::HttpCachingProxy	4	Proxying for HTTP requests only
//    QNetworkProxy::FtpCachingProxy	5
    switch(_type){
    case 2:
        ui->typeLabel->setText("NoProxy");
        break;
    case 0:
        ui->typeLabel->setText("DefaultProxy");
        break;
    case 1:
        ui->typeLabel->setText("Socks5Proxy");
        break;
    case 3:
        ui->typeLabel->setText("HttpProxy");
        break;
    case 4:
        ui->typeLabel->setText("HttpCachingProxy");
        break;
    case 5:
        ui->typeLabel->setText("FtpCachingProxy");
        break;
    }
};
void ProxyDescription::setHost(QString hots){
    _host=hots;
    ui->ipLabel->setText(_host);
};
void ProxyDescription::setPort(int port){
    _port=port;
    ui->portLabel->setText(QString().number(_port));
};

void ProxyDescription::setStatus(int status){
    _status=status;
//    static const int PENDING=0;
//    static const int WORKING=1;
//    static const int NOTWORKING=2;
    switch(_status){
    case 0:
        ui->statusLabel->setText("pending");
        break;
    case 1:
        ui->statusLabel->setText("working");
        break;
    case 2:
        ui->statusLabel->setText("dead");
        break;
    }
};
void ProxyDescription::addWait(QString page, QString time){
    ui->waitWidget->show();
    waitMap.insert(page,time);
    QVBoxLayout *layout;
    if(waitWidget==NULL){
        waitWidget=new QWidget(this);
        layout=new QVBoxLayout(waitWidget);
        layout->setMargin(2);
        layout->setSpacing(2);
        waitWidget->setLayout(layout);
        ui->scrollArea->setWidget(waitWidget);
    }else{
        layout=(QVBoxLayout*)waitWidget->layout();
        layout->takeAt(layout->count()-1);
    }
    layout->addWidget(new QLabel(page,waitWidget));
    layout->addWidget(new QLabel(time,waitWidget));
    layout->addStretch();

};
void ProxyDescription::clearWaits(){
    ui->waitWidget->hide();
    waitMap.clear();
};

ProxyDescription::~ProxyDescription()
{
    delete ui;
}
